package com.kitty.game.equip.handler;

import com.kitty.game.role.model.Role;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.enter.RespGeneralNotify;
import com.kitty.game.equip.message.RespOperateResult;
import com.kitty.game.equip.service.EquipService;
import com.kitty.game.utils.Const;
import com.kitty.listener.EventDispatcher;
import com.kitty.listener.EventType;
import com.kitty.listener.event.ChangeEquipEvent;

import com.kitty.mina.message.MessagePusher;
import com.kitty.game.attribute.AttrService;
import com.kitty.game.base.service.BagService;
import com.kitty.game.role.service.RoleService;
import org.springframework.stereotype.Component;

import static com.kitty.game.utils.Const.*;
import static com.kitty.game.utils.NotifyModules.NOTIFY_EQUIP_REFINE_OK;

/**
 * 装备打造实现类
 */
@Component
public abstract class UpgradeHandler {


    protected EquipService equipService = SpringUtils.getBean(EquipService.class);
    protected BagService bagService = SpringUtils.getBean(BagService.class);
    protected RoleService roleService = SpringUtils.getBean(RoleService.class);
    protected AttrService attrService = SpringUtils.getBean(AttrService.class);

    /**打造成功*/
    public static final boolean UPGRADE_SUCCESS = true;
    /**打造失败*/
    static final boolean UPGRADE_FAIL = false;
    /**打造的物品位置不正确*/
    /**未鉴定装备无法炼化*/
    static final String FAIL_MESSAGE ="#R{0}#n是未鉴定装备，无法进行炼化。";

    static final  String NO_RIGHT_POSITION ="很抱歉，请求{0}的物品#R{1}#n的类型不正确。";

    static final String ROLE_VACANCY_NOT_ENOUGH = "很抱歉，你的包裹空位不足，请整理后再来吧。";

    static final String NOT_FIND_CONFIG = "很抱歉，没有找到#R{0}#n的配置。";

    static final String HEISHUIJING = "超级黑水晶";

    static final String ERROR_OPTYPE ="操作的类型不正确。";

    /**判断打造的物品装备的部位是否符合条件*/
    boolean isRightPosition(byte equipPos){
        return equipPos == WEAPON || equipPos == HELMET || equipPos == ARMOR || equipPos == BOOT || equipPos == TALISMAN;
    }

    /**取属性的最大值*/
    int getMaxFieldByld(short attributeId, short equipLevel, byte pos){
        return attrService.getFiledMax(attributeId, equipLevel, pos);
    }
    /**
     * 开始打造 返回打造成功或者失败
     */
    public abstract String doUpgrade(Role role, Object...params);

    /**刷新装备*/
    public void refreshRoleEquip(Role role, RoleEquip roleEquip){
        equipService.refreshRoleEquip(role,roleEquip);

        RespGeneralNotify respGeneralNotify = new RespGeneralNotify();
        respGeneralNotify.setValue(roleEquip.getId() + "");
        respGeneralNotify.setNotify(getNotifyType());
        MessagePusher.pushMessage(role, respGeneralNotify);
    }

    int getNotifyType(){
       return NOTIFY_EQUIP_REFINE_OK;
    }

    /**
     * 入口
     */
    public abstract String acceptUpgrade(Role role, int upgradePos, String param);

    /**检查需要的资源是否足够*/
    public abstract String checkResource(Object...objects);

    /**背包是否有足够的空位
     */
    short getVacancy(Role role){
        return bagService.getPos(role, false);
    }

    /**打造需要的金钱
     * pos 只在鉴定的时候用*/
    public abstract int needCash(short level,int pos);

    /**
     * 发送打造结果 成功或者失败
     */
    public void pushOperateResult(Role role, boolean success) {
        RespOperateResult operateResult = new RespOperateResult();
        operateResult.setSuccess(success);
        operateResult.setType(getUpdateTypeStr());
        MessagePusher.pushMessage(role, operateResult);
    }
    /**
     * 分解装备获得精华
     */
    int getJewelryEssence(int level) {
        if (level == 80) {
            return 35;
        }
        if (level == 90) {
            return 60;
        }
        if (level == 100) {
            return 90;
        }
        if (level == 110) {
            return 120;
        }
        if (level == 120) {
            return 150;
        }
        if (level == 130) {
            return 180;
        }
        if (level == 140) {
            return 210;
        }
        if (level == 150) {
            return 240;
        }
        return 0;
    }
    /**
     * 取操作类型的字符串
     */
    public abstract String getUpdateTypeStr();

    public void pushExtraField(Role role , int pos){
        if (Const.MAIN_PAGE.contains((byte)pos) || Const.BACK_PAGE.contains((byte)pos)){
            /**产生一个装备改变事件*/
            EventDispatcher.getInstance().fireEvent(new ChangeEquipEvent(EventType.CHANGE_EQUIP, role, null));
        }
        EquipService equipService = SpringUtils.getBean(EquipService.class);
        RoleEquip roleEquip = equipService.getRoleEquipByPos(role, pos);
        if (roleEquip != null) {
            refreshRoleEquip(role, roleEquip);
        }
        if(getUpdateTypeStr()!=null && (getUpdateTypeStr().equals("suit_refine")
                || getUpdateTypeStr().equals("equip_evolve")
                || getUpdateTypeStr().equals("strengthen_jewelry"))){
            SpringUtils.getBroadcastService().sendUpdateAppear(role);
        }
    }

    /**继承消耗的元宝*/
    int getCostCoin(RoleEquip mEquip,RoleEquip oEquip){
        int maxLevel = Math.max(mEquip.queryLevel(), oEquip.queryLevel());
        double maxTypeRate =oEquip.queryUpgradeLevel()-mEquip.queryUpgradeLevel();
        return (int) Math.abs((Math.floor(Math.max(0.1, 0.97 * Math.pow(maxLevel / 120, 2) * 10000*maxTypeRate + 375) * maxTypeRate) + 700));
    }

    /**继承消耗的金钱*/
    int getCostMoney(RoleEquip mEquip,RoleEquip oEquip){
        int maxLevel = Math.max(mEquip.queryLevel(), oEquip.queryLevel());
        double maxTypeRate =oEquip.queryUpgradeLevel()-mEquip.queryUpgradeLevel();
        return (int) Math.abs((Math.floor(Math.max(0.1, 0.97 * Math.pow(maxLevel / 120, 2) * 8000000*maxTypeRate + 375) * maxTypeRate) + 700));
    }
}
